from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver import ActionChains
from action.element_action import ElementAction
from selenium.webdriver.remote.webelement import WebElement
from typing import Union


class MouseAction(ElementAction):
    '''鼠标操作
    '''

    def __init__(self, driver: WebDriver):
        self.driver = driver
        ElementAction(self.driver)

    def _move_to_el(self, feature: Union[tuple, WebElement]):
        '''移动到指定元素

        :param [tuple,WebElement] feature: 元素
        '''
        if isinstance(feature, tuple):
            return ActionChains(self.driver).move_to_element(self._find_el(feature)).perform()
        elif isinstance(feature, WebElement):
            return ActionChains(self.driver).move_to_element(feature).perform()

    def _move_by_offset(self, x: int, y: int):
        '''移动到固定xy
        '''
        # move_by_offset是将鼠标从上一次鼠标位置移到当前位置，故每次移回原位
        ActionChains(self.driver).move_by_offset(x, y).perform()
        ActionChains(self.driver).move_by_offset(-x, -y).perform()

    def _move_by_ele_offset(self, feature: Union[tuple, WebElement], x: int, y: int):
        '''以元素中心为基准进行偏移

        :param WebElement feature: 元素
        :param int x: x
        :param int y: y
        '''
        return ActionChains(self.driver).move_to_element_with_offset(self._find_el(feature), x, y).perform()

    def chains_click(self):
        '''点击
        '''
        ActionChains(self.driver).click().perform()
